package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Override
    public Boolean save(Category category) {

        Category dbCategory = categoryMapper.findByName(category.getName());
        if(dbCategory!=null){
            throw new BusinessException("菜品分类已存在");
        }
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        return categoryMapper.save(category)>0;
    }

    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<Category> pageDate = categoryMapper.findByPage();

        PageInfo<Category> categoryPageInfo = new PageInfo<>(pageDate);

        Page<Category> categoryPage = new Page<>();
        categoryPage.setRecords(categoryPageInfo.getList());
        categoryPage.setTotal(categoryPageInfo.getTotal());
        return categoryPage;
    }

    @Override
    public R<String> deleteById(Long id) {
        Integer dishNum = dishMapper.selectDish(id);
        Integer setMealNum = setmealMapper.selectSetmeal(id);
        if(dishNum>0){
            throw new BusinessException("该分类下还有菜品，不能删除");
        }
        if(setMealNum>0){
            throw new BusinessException("该分类下还有套餐，不能删除");
        }
        Integer deleteNum = categoryMapper.deleteByid(id);
        if(deleteNum>0){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @Override
    public Boolean update(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        Category dbcate = categoryMapper.findByName2(category);
        if(dbcate!=null){
            throw new BusinessException("该分类名字已存在，请重新修改");
        }else{
            Integer num = categoryMapper.update(category);
            return num > 0;
        }
    }

    /**
     * 查询所有分类的数据
     * @param o
     * @return
     */
    @Override
    public List<Category> findAll(Object o) {
        return categoryMapper.findByPage();
    }

    @Override
    public List<Category> findAllByType(Integer type) {
        return categoryMapper.findAllByType(type);
    }
}
